package com.shangcheng.controller;

import com.shangcheng.common.Result;
import com.shangcheng.entity.TCustomer;
import com.shangcheng.service.TCustemerServiceImpl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/public"})
@RestController
public class PublicCustomerController {
    @Autowired
    private TCustemerServiceImpl service;
    @PostMapping({"/login"})
    public Result<Map<String, Object>> login(@RequestBody Map<String, String> loginMap) {
        Result<Map<String, Object>> re = Result.ok((Map<String, Object>) null);
        String userName = (String)loginMap.get("username");
        String passWord = (String)loginMap.get("password");
        TCustomer retCustmer = this.service.queryByUsername(userName);
        if (retCustmer == null) {
            re.setCode(100);
            re.setMessage("用户名错误！");
        } else if (!retCustmer.getPassword().equals(passWord)) {
            re.setCode(101);
            re.setMessage("密码错误！");
        } else {
            Map<String, Object> map = new HashMap();
            map.put("name", retCustmer.getName());
            map.put("id", retCustmer.getId());
            map.put("username", retCustmer.getUserName());
            map.put("address",retCustmer.getAddress());
            map.put("token", this.service.createTocken(retCustmer));
            re.setData(map);
            re.setMessage("登陆成功！");
        }
        return re;
    }
    @PostMapping("/regist")
    public Result<Map<String, Object>> regist(@RequestBody TCustomer customer) {
        Result<Map<String, Object>> re = Result.ok((Map<String, Object>) null);
        TCustomer reCustmer = this.service.register(customer);
        if (reCustmer == null) {
            re.setCode(100);
            re.setMessage("信息不能为空");
        } else if (reCustmer.getName()==""||reCustmer.getPassword()==""||reCustmer.getUserName()==""||reCustmer.getAge()==null||reCustmer.getSex()==null||reCustmer.getAddress()==""||reCustmer.getIdCard()=="") {
            re.setCode(100);
            re.setMessage("信息不能为空");
        }else {
            Map<String, Object> map = new HashMap();
            map.put("name", customer.getName());
            map.put("password", customer.getPassword());
            map.put("username", customer.getUserName());
            map.put("age", customer.getAge());
            map.put("address", customer.getAddress());
            map.put("sex",customer.getSex());
            map.put("idcard",customer.getIdCard());
            map.put("token", this.service.createTocken(reCustmer));
            re.setData(map);
            re.setMessage("注册成功");
        }
            return re;
        }
    }
